package gl

import org.lwjgl.opengl.GL20.*
import java.nio.ByteBuffer

object Debugger {

    val data = mutableMapOf<String, Any>()

    operator fun set(key: String, value: Any) {
        data[key] = value
    }

    operator fun <T> get(key: String) = data[key] as T?

    fun <T> getOr(key: String, default: T) = get(key) ?: default

    fun getByteBuffers(target: Int, size: Int, offset: Int = 0): ByteArray {
        val buffer = glMapBuffer(target, GL_READ_ONLY) ?: return byteArrayOf()
        val array = ByteArray(size)
        buffer.get(array, offset, size)
        glUnmapBuffer(target)
        return array
    }

    fun getIntBuffers(target: Int, size: Int, offset: Int = 0): IntArray {
        val buffer = glMapBuffer(target, GL_READ_ONLY) ?: return intArrayOf()
        val array = IntArray(size)
        buffer.asIntBuffer().get(array, offset, size)
        glUnmapBuffer(target)
        return array
    }

    fun getFloatBuffers(target: Int, size: Int, offset: Int = 0): FloatArray {
        val buffer = glMapBuffer(target, GL_READ_ONLY) ?: return floatArrayOf()
        val array = FloatArray(size)
        buffer.asFloatBuffer().get(array, offset, size)
        glUnmapBuffer(target)
        return array
    }
}